using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
public class Shop_Member_Shop_AddReview : Member, IRequiresSessionState
{
	protected Label Show_Tips;
	protected HyperLink ReviewTo;
	protected Label LabTransSize;
	protected HyperLink GiveReview;
	protected RadioButtonList RadioStart;
	protected TextBox TxtTitle;
	protected TextBox TxtContent;
	protected Button ButSave;
	protected Panel ShowReview;
	protected HiddenField H_ComeUrl;
	protected HiddenField H_ReviewsID;
	protected HiddenField H_Action;
	protected HiddenField hfShopID;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			if (base.Request["Action"] != null && base.Request["Action"] == "modify")
			{
				this.BindModifyPage();
			}
			else
			{
				this.BindAddPage();
			}
		}
	}
	protected void BindModifyPage()
	{
		DataTable dataTable = this.conn.GetDataTable(string.Concat(new string[]
		{
			"select * from HLShop_Review where UserID=",
			base.user.UserID.ToString(),
			" and ProductID=",
			Fetch.Get("ProductID"),
			" and  ReviewerID=",
			Fetch.Get("ReviewerID"),
			" and  orderID=",
			Fetch.Get("itemid")
		}));
		if (dataTable.Rows.Count >= 1)
		{
			DataRow dataRow = dataTable.Rows[0];
			int num = 0;
			if (!string.IsNullOrEmpty(dataRow["ReviewUpdateTimes"].ToString()))
			{
				num = (int)dataRow["ReviewUpdateTimes"];
			}
			string a = dataRow["UserID"].ToString();
			DateTime dateTime = (DateTime)dataRow["ReviewDate"];
			this.ReviewTo.Text = dataRow["ReviewerName"].ToString();
			this.GiveReview.Text = Fetch.Get("ProductName");
			this.RadioStart.SelectedValue = dataRow["ReviewStar"].ToString();
			this.TxtTitle.Text = dataRow["ReviewTitle"].ToString();
			this.TxtContent.Text = dataRow["ReviewContent"].ToString();
			this.ButSave.Text = "修改评价";
			this.H_ReviewsID.Value = dataRow["ReviewsID"].ToString();
			this.H_Action.Value = "modify";
			if (num < 1 && a == base.user.UserID.ToString() && DateTime.Now < dateTime.AddDays(30.0))
			{
				this.Show_Tips.Text = "注意：<br><ul><li>评价只能被修改一次，请慎重决定！</li></ul>";
			}
			else
			{
				this.Show_Tips.Text = "对不起，您不能修改评价，有可能是因为以下情况的一种或几种：<br><ul><li>1、评价已被修改一次，不允许重复修改！</li><li>2、你没有浏览该评价的权限</li><li>3、评价的修改期限已过</li></ul>";
				this.ButSave.Visible = false;
				this.RadioStart.Enabled = false;
				this.TxtTitle.Enabled = false;
				this.TxtContent.Enabled = false;
			}
		}
		else
		{
			this.WriteErrMsg("", "该订单未完成,还不能评价", "");
		}
	}
	protected void BindAddPage()
	{
		string value = base.Server.HtmlEncode(base.Request["ItemID"]);
		string value2 = base.Server.HtmlEncode(base.Request["ProductID"]);
		string text = base.Server.HtmlEncode(base.Request["ProductName"]);
		string value3 = base.Server.HtmlEncode(base.Request["ReviewerID"]);
		string text2 = base.Server.HtmlEncode(base.Request["ReviewerName"]);
		int orderID = 0;
		int productID = 0;
		int salerID = 0;
		int buyerID = 0;
		int userID = base.user.UserID;
		int num = 0;
		this.GiveReview.Text = text;
		this.GiveReview.NavigateUrl = "../ShowProduct.aspx?ProductID=" + base.Request["ProductID"];
		this.ReviewTo.Text = text2;
		if (base.Request["TransSize"] != null)
		{
			num = int.Parse(base.Request["TransSize"]);
			if (num == 1)
			{
				this.LabTransSize.Text = "作为买家";
				buyerID = Convert.ToInt32(value3);
				salerID = userID;
			}
			else
			{
				this.LabTransSize.Text = "作为卖家";
				salerID = Convert.ToInt32(value3);
				buyerID = userID;
			}
		}
		try
		{
			orderID = Convert.ToInt32(value);
			productID = Convert.ToInt32(value2);
		}
		catch
		{
			this.WriteErrMsg("", "该订单未完成,还不能评价", "");
		}
		if (this.CheckReview(orderID, productID, salerID, buyerID, num, userID))
		{
			string text3 = "<ul><li>评价须知：</li><li>1、请一定在确认已经签收卖家的商品时才给卖家好评。</li><li>2、如果您对卖家的发货速度不满意，请根据自己的体会给予中评或者差评。您的感受将对其他的会员起到参考作用，请客观仔细地评价。</li><li>3、每个自然月中，相同买家和卖家之间的在线支付交易评价计分不超过6分（以支付宝交易创建的时间计算）。超出计分规则范围的评价将不计分。</li></ul>";
			this.Show_Tips.Text = text3;
			this.ShowReview.Visible = true;
		}
		else
		{
			string text3 = "对不起，您不能进行评价，有可能是因为以下情况的一种或几种：<br>";
			text3 += "<ul><li>1、不在评价期内，交易完成后当天及之后45天之内可作评价；</li><li>2、您已经针对该交易的买(卖)家进行了评价；</li><li>3、您已经与该买家交易此商品超过一次，并已针对该买(卖)家进行过评价（对同一商品同一买(卖)家只能评价一次）</li></ul>";
			this.Show_Tips.Text = text3;
			this.ShowReview.Visible = false;
		}
	}
	protected void ButSave_Click(object sender, EventArgs e)
	{
		string value = Text.ReplaceBadChar(base.Request["ItemID"]);
		string value2 = Text.ReplaceBadChar(base.Request["ProductID"]);
		string text = Text.ReplaceBadChar(base.Request["ProductName"]);
		string value3 = Text.ReplaceBadChar(base.Request["ReviewerID"]);
		string value4 = Text.ReplaceBadChar(base.Request["ReviewerName"]);
		string text2 = Text.ReplaceBadChar(this.TxtTitle.Text);
		string text3 = Text.ReplaceBadChar(this.TxtContent.Text);
		if (text2.Length > 25)
		{
			base.Response.Write("标题过长");
			base.Response.End();
		}
		Shop_Review shop_Review = new Shop_Review();
		if (this.H_Action.Value == "modify")
		{
			int num = Convert.ToInt32(this.H_ReviewsID.Value);
			num = int.Parse(this.RadioStart.SelectedValue);
			if (shop_Review.UpdateReview(Convert.ToInt32(this.H_ReviewsID.Value), int.Parse(this.RadioStart.SelectedValue), text2, text3))
			{
				this.WriteSuccessMsg("", Language.Get("Member/Order/OrderEditReview"), "");
			}
		}
		else
		{
			string commandText = "INSERT INTO [HLShop_Review]([ShopID],[ProductID],[UserID],[OrderID],[UserName],[ReviewTitle],[ReviewContent],[ReviewStar],[ReviewDate],[TransSize],[ReviewerID],[ReviewerName],[ReviewStatus]) VALUES(@ShopID,@ProductID,@UserID,@OrderID,@UserName,@ReviewTitle,@ReviewContent,@ReviewStar,@ReviewDate,@TransSize,@ReviewerID,@ReviewerName,@ReviewStatus)";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@ShopID", DbType.Int32, Convert.ToInt32(this.hfShopID.Value)),
				this.conn.CreateParameter("@ProductID", DbType.Int32, Convert.ToInt32(value2)),
				this.conn.CreateParameter("@UserID", DbType.Int32, base.user.UserID),
				this.conn.CreateParameter("@OrderID", DbType.Int32, Convert.ToInt32(value)),
				this.conn.CreateParameter("@UserName", DbType.String, base.user.UserName),
				this.conn.CreateParameter("@ReviewTitle", DbType.String, text2),
				this.conn.CreateParameter("@ReviewContent", DbType.String, text3),
				this.conn.CreateParameter("@ReviewStar", DbType.Int32, int.Parse(this.RadioStart.SelectedValue)),
				this.conn.CreateParameter("@ReviewDate", DbType.DateTime, DateTime.Now.Date),
				this.conn.CreateParameter("@TransSize", DbType.Int32, Convert.ToInt32(base.Request["TransSize"])),
				this.conn.CreateParameter("@ReviewerID", DbType.Int32, Convert.ToInt32(value3)),
				this.conn.CreateParameter("@ReviewerName", DbType.String, value4),
				this.conn.CreateParameter("@ReviewStatus", DbType.Int32, 1)
			};
			int num2 = this.conn.Execute(commandText, parameters);
			if (num2 > 0)
			{
				this.WriteSuccessMsg("", Language.Get("Member/Order/OrderAddReview"), "");
			}
		}
	}
	private bool CheckReview(int OrderID, int ProductID, int SalerID, int BuyerID, int ReviewType, int ReviewerID)
	{
		Shop_Order shop_Order = new Shop_Order();
		DataTable orderByOrderID = shop_Order.GetOrderByOrderID(OrderID);
		bool result;
		if (orderByOrderID.Rows.Count > 0)
		{
			if (orderByOrderID.Rows[0]["ShopID"] != DBNull.Value)
			{
				this.hfShopID.Value = orderByOrderID.Rows[0]["ShopID"].ToString();
			}
			string a = orderByOrderID.Rows[0]["BuyerReview"].ToString();
			string a2 = orderByOrderID.Rows[0]["SalerReview"].ToString();
			int num = (int)orderByOrderID.Rows[0]["ProductID"];
			int num2 = (int)orderByOrderID.Rows[0]["SalerID"];
			int num3 = (int)orderByOrderID.Rows[0]["UserID"];
			string a3 = orderByOrderID.Rows[0]["Status"].ToString();
			DateTime dateTime = (DateTime)orderByOrderID.Rows[0]["UpdateTime"];
			DateTime t = dateTime;
			DateTime t2 = dateTime.AddDays(45.0);
			result = (num2 == SalerID && num3 == BuyerID && num == ProductID && (ReviewType != 1 || !(a2 == "1")) && (ReviewType != 0 || !(a == "1")) && ((ReviewType == 1 && num2 == ReviewerID) || (ReviewType == 0 && num3 == ReviewerID)) && !(a3 != "2") && !(DateTime.Now > t2) && !(t > DateTime.Now));
		}
		else
		{
			result = false;
		}
		return result;
	}
}
